<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ConfigItemResource\Pages;
use App\Models\ConfigItem;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\TextInputColumn;
use Filament\Tables\Table;

class ConfigItemResource extends Resource
{
    protected static ?string $model = ConfigItem::class;

    protected static ?string $navigationIcon = 'heroicon-o-wrench-screwdriver';

    protected static ?int $navigationSort = 1;

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.website_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.website_group.config_item');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.website_group.config_item');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Select::make('config_id')
                    ->label(__('config-item.Config_id'))
                    ->relationship(name: 'config', titleAttribute: 'name')
                    ->preload()
                    ->searchable()
                    ->required(),
                TextInput::make('name')->label(__('config-item.Name'))->required(),
                Textarea::make('content')->label(__('config-item.Content'))->rows(4)->required(),
                TextInput::make('sort')->label(__('config-item.Sort'))->integer(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('config.name')
                    ->label(__('config-item.Config_id'))
                    ->badge()
                    ->color('success')
                    ->alignCenter(),
                TextInputColumn::make('name')->label(__('config-item.Name')),
                TextColumn::make('content')->label(__('config-item.Content'))->copyable(),
                TextColumn::make('sort')->label(__('config-item.Sort'))->alignCenter(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('sort')
            ->reorderable('sort');
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListConfigItems::route('/'),
        ];
    }
}
